<?php

namespace App\Http\Controllers\Admin;

use App\Model\Admin;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;

class EntryController extends Controller
{

    public function __construct()
    {
        $this->middleware('admin.auth')->except(['login','index','logout']);
    }

    //后台首页登录视图

    public function index(){
        return view('admin');
    }
    //
    public function login(Request $request){

        $status = Auth::guard('admin')->attempt([
            'username'=>$request->input('username'),
            'password'=>$request->input('password')
        ]);
        if($status){
            $user = Admin::where('username',$request->input('username'))->first();
            $user -> api_token = str_random('64');
            if($user->save()){
                return response()->json(
                    [
                        'code'=>1,
                        'msg'=>'登录成功',
                        'access_token'=>$user -> api_token
                    ]
                );
            }

        }else{
            return response()->json(['code'=>0,'msg'=>'用户名或者密码错误']);
        }
    }

    public function logout(){
        Auth::guard('admin')->logout();
        return response()->json(['code'=>1,'msg'=>'退出登录成功']);
    }
}
